package com.easy.sk.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.SeckillGoods;
import com.easy.common.service.ISeckillGoodsService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/seckill/goods")
public class SeckillGoodsController {

    @Autowired
    ISeckillGoodsService seckillGoodsService;

    // 根据id获取秒杀商品信息
    @GetMapping("/id/{id}")
    public Result getSeckillGoodsById(@PathVariable Integer id) {
        try {
            return Result.success(seckillGoodsService.getSeckillGoodsById(id));
        } catch (Exception e) {
            return Result.fail(500, "获取秒杀商品信息失败: " + e.getMessage());
        }
    }

    // 根据商品id获取秒杀商品信息
    @GetMapping("/goodsid/{goodsId}")
    public Result getSeckillGoodsByGoodsId(@PathVariable Integer goodsId) {
        try {
            return Result.success(seckillGoodsService.getSeckillGoodsByGoodsId(goodsId));
        } catch (Exception e) {
            return Result.fail(500, "获取秒杀商品信息失败: " + e.getMessage());
        }
    }

    // 添加秒杀商品
    @PostMapping("/add")
    public Result addSeckillGoods(@RequestBody SeckillGoods seckillGoods) {
        try {
            int result = seckillGoodsService.addSeckillGoods(seckillGoods);
            System.out.println("---------" + result);
            if (result > 0) {
                return Result.success("秒杀商品添加成功");
            } else {
                return Result.fail(500, "秒杀商品添加失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "秒杀商品添加失败: " + e.getMessage());
        }
    }

    // 更新秒杀商品
    @PutMapping("/update")
    public Result updateSeckillGoods(@RequestBody SeckillGoods seckillGoods) {
        try {
            int result = seckillGoodsService.updateSeckillGoods(seckillGoods);
            if (result > 0) {
                return Result.success("秒杀商品更新成功");
            } else {
                return Result.fail(500, "秒杀商品更新失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "秒杀商品更新失败: " + e.getMessage());
        }
    }

    // 删除秒杀商品(软删除)
    @DeleteMapping("/delete/{id}")
    public Result deleteSeckillGoods(@PathVariable Integer id) {
        try {
            int result = seckillGoodsService.deleteSeckillGoods(id);
            if (result > 0) {
                return Result.success("秒杀商品删除成功");
            } else {
                return Result.fail(500, "秒杀商品删除失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "秒杀商品删除失败: " + e.getMessage());
        }
    }

    // 切换秒杀商品状态
    @PostMapping("/status/{id}")
    public Result reverseStatus(@PathVariable Integer id) {
        try {
            int result = seckillGoodsService.reverseStatus(id);
            if (result > 0) {
                SeckillGoods seckillGoods = seckillGoodsService.getSeckillGoodsById(id);
                String status;
                switch (seckillGoods.getStatus()) {
                    case 0: status = "未开始"; break;
                    case 1: status = "进行中"; break;
                    case 2: status = "已结束"; break;
                    default: status = "未知状态";
                }
                return Result.success("秒杀商品状态已更新为: " + status);
            } else {
                return Result.fail(500, "状态更新失败");
            }
        } catch (Exception e) {
            return Result.fail(500, "状态更新失败: " + e.getMessage());
        }
    }

    // 获取秒杀商品列表
    @GetMapping("/list")
    public Result getSeckillGoodsList(SeckillGoods seckillGoods) {
        try {
            return Result.success(seckillGoodsService.getAll(seckillGoods));
        } catch (Exception e) {
            return Result.fail(500, "获取秒杀商品列表失败: " + e.getMessage());
        }
    }

    // 获取秒杀商品分页列表
    @GetMapping("/page")
    public Result getSeckillGoodsPage(SeckillGoods seckillGoods, Integer current, Integer size) {
        try {
            IPage<SeckillGoods> page = new Page<>(current != null ? current : 1, size != null ? size : 10);
            return Result.success("查询成功", seckillGoodsService.getPage(seckillGoods, page));
        } catch (Exception e) {
            return Result.fail(500, "获取秒杀商品分页列表失败: " + e.getMessage());
        }
    }
}
